Imports System
Imports System.Collections.Generic
Imports System.ComponentModel
Imports System.Data
Imports System.Drawing
Imports System.Text
Imports System.Windows.Forms

Namespace SerialPortProfileApplication

	Public Partial Class PortSettings
		Inherits Form
		Private _baudRate As Integer
		Private _dataBits As Integer
        Private _parity As System.IO.Ports.Parity
		Private _parityReplace As Byte
        Private _stopBits As System.IO.Ports.StopBits
		Private _readBufferSize As Integer
		Private _readTimeout As Integer
		Private _writeBufferSize As Integer
		Private _writeTimeout As Integer
		Private _encoding As Encoding
        Private sppManager1 As C4F.DevKit.Bluetooth.SerialPortProfile.BluetoothSPPManager

        Public Sub New(ByVal sppManager As C4F.DevKit.Bluetooth.SerialPortProfile. _
                                                                                BluetoothSPPManager)
            InitializeComponent()
            sppManager1 = sppManager

            With cmbEncoding
                .Items.Add("ASCII")
                .Items.Add("UTF7")
                .Items.Add("UTF8")
                .Items.Add("UTF32")
            End With

            With cmbParity
                cmbParity.Items.Add(System.IO.Ports.Parity.Even)
                cmbParity.Items.Add(System.IO.Ports.Parity.Mark)
                cmbParity.Items.Add(System.IO.Ports.Parity.None)
                cmbParity.Items.Add(System.IO.Ports.Parity.Odd)
                cmbParity.Items.Add(System.IO.Ports.Parity.Space)
            End With

            With cmbStopBits
                cmbStopBits.Items.Add(System.IO.Ports.StopBits.None)
                cmbStopBits.Items.Add(System.IO.Ports.StopBits.One)
                cmbStopBits.Items.Add(System.IO.Ports.StopBits.OnePointFive)
                cmbStopBits.Items.Add(System.IO.Ports.StopBits.Two)
            End With

            txtBoxBaudRate.Text = sppManager1.BaudRate.ToString()
            txtBoxDataBits.Text = sppManager1.DataBits.ToString()
            txtBoxParityReplace.Text = sppManager1.ParityReplace.ToString()
            txtBoxReadBufSize.Text = sppManager1.ReadBufferSize.ToString()
            txtBoxReadTimeOut.Text = sppManager1.ReadTimeout.ToString()
            txtBoxWriteTimeOut.Text = sppManager1.WriteTimeout.ToString()
            txtWriteBufSize.Text = sppManager1.WriteBufferSize.ToString()

            With cmbEncoding
                If TypeOf sppManager1.Encoding Is ASCIIEncoding Then
                    .SelectedIndex = 0
                End If

                If TypeOf sppManager1.Encoding Is UTF32Encoding Then
                    .SelectedIndex = 3
                End If

                If TypeOf sppManager1.Encoding Is UTF7Encoding Then
                    .SelectedIndex = 1
                End If

                If TypeOf sppManager1.Encoding Is UTF8Encoding Then
                    .SelectedIndex = 2
                End If

            End With

            With cmbStopBits
                If sppManager1.StopBits = System.IO.Ports.StopBits.None Then
                    .SelectedIndex = 0
                End If

                If sppManager1.StopBits = System.IO.Ports.StopBits.One Then
                    .SelectedIndex = 1
                End If

                If sppManager1.StopBits = System.IO.Ports.StopBits.OnePointFive Then
                    .SelectedIndex = 2
                End If

                If sppManager1.StopBits = System.IO.Ports.StopBits.Two Then
                    .SelectedIndex = 3
                End If

            End With

            With cmbParity
                If sppManager1.Parity = System.IO.Ports.Parity.Even Then
                    .SelectedIndex = 0
                End If
                If sppManager1.Parity = System.IO.Ports.Parity.Mark Then
                    .SelectedIndex = 1
                End If

                If sppManager1.Parity = System.IO.Ports.Parity.None Then
                    .SelectedIndex = 2
                End If

                If sppManager1.Parity = System.IO.Ports.Parity.Odd Then
                    .SelectedIndex = 3
                End If

                If sppManager1.Parity = System.IO.Ports.Parity.Space Then
                    .SelectedIndex = 3
                End If

            End With

        End Sub


        Private Sub btnCancel_Click(ByVal sender As Object, ByVal e As EventArgs) Handles btnCancel.Click
            Dispose()
        End Sub

        Private Sub btnOk_Click(ByVal sender As Object, ByVal e As EventArgs) Handles btnOk.Click
            Try
                If String.IsNullOrEmpty(txtBoxBaudRate.Text) Then
                    Microsoft.VisualBasic.MsgBox("Please enter a valid value for Baud Rate.", _
                                    Microsoft.VisualBasic.MsgBoxStyle.OkOnly Or _
                                             Microsoft.VisualBasic.MsgBoxStyle.Critical, _
                                             "Port Settings")

                    txtBoxBaudRate.Focus()
                End If

                If String.IsNullOrEmpty(txtBoxDataBits.Text) Then
                    Microsoft.VisualBasic.MsgBox("Please enter a valid value for Data Bits.", _
                                    Microsoft.VisualBasic.MsgBoxStyle.OkOnly Or _
                                             Microsoft.VisualBasic.MsgBoxStyle.Critical, _
                                             "Port Settings")
                    txtBoxDataBits.Focus()
                End If

                If cmbParity.SelectedIndex = -1 Then
                    Microsoft.VisualBasic.MsgBox("Please enter a valid value for Parity.", _
                                    Microsoft.VisualBasic.MsgBoxStyle.OkOnly Or _
                                             Microsoft.VisualBasic.MsgBoxStyle.Critical, _
                                             "Port Settings")
                    cmbParity.Focus()
                End If

                If String.IsNullOrEmpty(txtBoxParityReplace.Text) Then
                    Microsoft.VisualBasic.MsgBox("Please enter a valid value for Parity Replace.", _
                                    Microsoft.VisualBasic.MsgBoxStyle.OkOnly Or _
                                             Microsoft.VisualBasic.MsgBoxStyle.Critical, _
                                             "Port Settings")
                    txtBoxParityReplace.Focus()
                End If

                If String.IsNullOrEmpty(txtBoxReadBufSize.Text) Then
                    Microsoft.VisualBasic.MsgBox("Please enter a valid value for Read Buffer Size.", _
                                   Microsoft.VisualBasic.MsgBoxStyle.OkOnly Or _
                                             Microsoft.VisualBasic.MsgBoxStyle.Critical, _
                                             "Port Settings")
                    txtBoxReadBufSize.Focus()
                End If

                If cmbStopBits.SelectedIndex = -1 Then
                    Microsoft.VisualBasic.MsgBox("Please enter a valid value for Stop Bits.", _
                                    Microsoft.VisualBasic.MsgBoxStyle.OkOnly Or _
                                             Microsoft.VisualBasic.MsgBoxStyle.Critical, _
                                             "Port Settings")
                    cmbStopBits.Focus()
                End If

                If String.IsNullOrEmpty(txtBoxReadTimeOut.Text) Then
                    Microsoft.VisualBasic.MsgBox("Please enter a valid value for Read TimeOut.", _
                                    Microsoft.VisualBasic.MsgBoxStyle.OkOnly Or _
                                             Microsoft.VisualBasic.MsgBoxStyle.Critical, _
                                             "Port Settings")
                    txtBoxReadTimeOut.Focus()
                End If

                If String.IsNullOrEmpty(txtBoxWriteTimeOut.Text) Then
                    Microsoft.VisualBasic.MsgBox("Please enter a valid value for Write TimeOut", _
                                    Microsoft.VisualBasic.MsgBoxStyle.OkOnly Or _
                                             Microsoft.VisualBasic.MsgBoxStyle.Critical, _
                                             "Port Settings")
                    txtBoxWriteTimeOut.Focus()
                End If

                If cmbEncoding.SelectedIndex = -1 Then
                    Microsoft.VisualBasic.MsgBox("Please enter a valid value for Encoding.", _
                                   Microsoft.VisualBasic.MsgBoxStyle.OkOnly Or _
                                             Microsoft.VisualBasic.MsgBoxStyle.Critical, _
                                             "Port Settings")
                    cmbEncoding.Focus()
                Else

                    With sppManager1
                        _baudRate = Convert.ToInt32(txtBoxBaudRate.Text)
                        .BaudRate = _baudRate
                        _dataBits = Convert.ToInt32(txtBoxDataBits.Text)
                        .DataBits = _dataBits
                        _parityReplace = Convert.ToByte(txtBoxParityReplace.Text)
                        .ParityReplace = _parityReplace
                        _readBufferSize = Convert.ToInt32(txtBoxReadBufSize.Text)
                        .ReadBufferSize = _readBufferSize
                        _readTimeout = Convert.ToInt32(txtBoxReadTimeOut.Text)
                        .ReadTimeout = _readTimeout
                        _writeBufferSize = Convert.ToInt32(txtWriteBufSize.Text)
                        .WriteBufferSize = _writeBufferSize
                        _writeTimeout = Convert.ToInt32(txtBoxWriteTimeOut.Text)
                        .WriteTimeout = _writeTimeout
                    End With

                    Select Case cmbEncoding.SelectedIndex
                        Case 0
                            _encoding = New ASCIIEncoding()
                            Exit Select
                        Case 1
                            _encoding = New UTF7Encoding()
                            Exit Select
                        Case 2
                            _encoding = New UTF8Encoding()
                            Exit Select
                        Case 3
                            _encoding = New UTF32Encoding()
                            Exit Select
                    End Select

                    sppManager1.Encoding = _encoding

                    Select Case cmbParity.SelectedIndex
                        Case 0
                            _parity = System.IO.Ports.Parity.Even
                            Exit Select
                        Case 1
                            _parity = System.IO.Ports.Parity.Mark
                            Exit Select
                        Case 2
                            _parity = System.IO.Ports.Parity.None
                            Exit Select
                        Case 3
                            _parity = System.IO.Ports.Parity.Odd
                            Exit Select
                        Case 4
                            _parity = System.IO.Ports.Parity.Space
                            Exit Select
                    End Select

                    sppManager1.Parity = _parity

                    Select Case cmbStopBits.SelectedIndex
                        Case 0
                            _stopBits = System.IO.Ports.StopBits.None
                            Exit Select
                        Case 1
                            _stopBits = System.IO.Ports.StopBits.One
                            Exit Select
                        Case 2
                            _stopBits = System.IO.Ports.StopBits.OnePointFive
                            Exit Select
                        Case 3
                            _stopBits = System.IO.Ports.StopBits.Two
                            Exit Select
                    End Select

                    sppManager1.StopBits = _stopBits
                End If

            Catch ex As Exception
                Throw ex
            Finally
                Dispose()
            End Try

        End Sub

    End Class

End Namespace
